package com.knowledgegraph.neo4j.controller;

import com.knowledgegraph.common.Result;
import com.knowledgegraph.neo4j.entity.GraphRelationship;
import com.knowledgegraph.neo4j.service.Neo4jRelationshipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Neo4j 关系控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/neo4j/relationships")
@Tag(name = "Neo4j关系管理", description = "Neo4j图数据库关系管理接口")
public class Neo4jRelationshipController {

    @Autowired
    private Neo4jRelationshipService relationshipService;

    @PostMapping
    @Operation(summary = "创建关系", description = "在两个节点之间创建关系")
    public Result<GraphRelationship> createRelationship(
            @RequestParam Long startNodeId,
            @RequestParam Long endNodeId,
            @RequestParam String type,
            @RequestParam(required = false) String description,
            @RequestBody(required = false) Map<String, Object> properties,
            @RequestParam Long graphId) {
        try {
            GraphRelationship relationship = relationshipService.createRelationship(
                startNodeId, endNodeId, type, description, properties, graphId);
            return Result.success(relationship);
        } catch (Exception e) {
            log.error("创建关系失败", e);
            return Result.error("创建关系失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询关系", description = "根据ID查询关系详情")
    public Result<GraphRelationship> getRelationship(@PathVariable Long id) {
        try {
            GraphRelationship relationship = relationshipService.findById(id);
            if (relationship == null) {
                return Result.error("关系不存在");
            }
            return Result.success(relationship);
        } catch (Exception e) {
            log.error("查询关系失败", e);
            return Result.error("查询关系失败: " + e.getMessage());
        }
    }

    @GetMapping("/graph/{graphId}")
    @Operation(summary = "查询图谱关系", description = "根据图谱ID查询所有关系")
    public Result<List<GraphRelationship>> getRelationshipsByGraphId(@PathVariable Long graphId) {
        try {
            List<GraphRelationship> relationships = relationshipService.findByGraphId(graphId);
            return Result.success(relationships);
        } catch (Exception e) {
            log.error("查询图谱关系失败", e);
            return Result.error("查询图谱关系失败: " + e.getMessage());
        }
    }

    @GetMapping("/type/{type}")
    @Operation(summary = "按类型查询关系", description = "根据关系类型查询关系")
    public Result<List<GraphRelationship>> getRelationshipsByType(@PathVariable String type) {
        try {
            List<GraphRelationship> relationships = relationshipService.findByType(type);
            return Result.success(relationships);
        } catch (Exception e) {
            log.error("按类型查询关系失败", e);
            return Result.error("按类型查询关系失败: " + e.getMessage());
        }
    }

    @GetMapping("/between")
    @Operation(summary = "查询节点间关系", description = "查询两个节点之间的关系")
    public Result<List<GraphRelationship>> getRelationshipsBetweenNodes(
            @RequestParam Long startNodeId,
            @RequestParam Long endNodeId) {
        try {
            List<GraphRelationship> relationships = relationshipService.findBetweenNodes(startNodeId, endNodeId);
            return Result.success(relationships);
        } catch (Exception e) {
            log.error("查询节点间关系失败", e);
            return Result.error("查询节点间关系失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新关系", description = "更新关系信息")
    public Result<GraphRelationship> updateRelationship(@PathVariable Long id, @RequestBody GraphRelationship relationship) {
        try {
            relationship.setId(id);
            GraphRelationship updated = relationshipService.updateRelationship(relationship);
            return Result.success(updated);
        } catch (Exception e) {
            log.error("更新关系失败", e);
            return Result.error("更新关系失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除关系", description = "根据ID删除关系")
    public Result<Void> deleteRelationship(@PathVariable Long id) {
        try {
            relationshipService.deleteRelationship(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除关系失败", e);
            return Result.error("删除关系失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/graph/{graphId}")
    @Operation(summary = "删除图谱关系", description = "删除指定图谱下的所有关系")
    public Result<Void> deleteRelationshipsByGraphId(@PathVariable Long graphId) {
        try {
            relationshipService.deleteByGraphId(graphId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除图谱关系失败", e);
            return Result.error("删除图谱关系失败: " + e.getMessage());
        }
    }

    @GetMapping("/graph/{graphId}/count")
    @Operation(summary = "统计关系数量", description = "统计指定图谱下的关系数量")
    public Result<Long> countRelationshipsByGraphId(@PathVariable Long graphId) {
        try {
            Long count = relationshipService.countByGraphId(graphId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计关系数量失败", e);
            return Result.error("统计关系数量失败: " + e.getMessage());
        }
    }

    @GetMapping("/strength")
    @Operation(summary = "按强度查询关系", description = "根据强度范围查询关系")
    public Result<List<GraphRelationship>> getRelationshipsByStrength(
            @RequestParam(defaultValue = "0.0") Double minStrength,
            @RequestParam(defaultValue = "1.0") Double maxStrength) {
        try {
            List<GraphRelationship> relationships = relationshipService.findByStrengthBetween(minStrength, maxStrength);
            return Result.success(relationships);
        } catch (Exception e) {
            log.error("按强度查询关系失败", e);
            return Result.error("按强度查询关系失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建关系", description = "批量创建多个关系")
    public Result<List<GraphRelationship>> batchCreateRelationships(@RequestBody List<GraphRelationship> relationships) {
        try {
            List<GraphRelationship> created = relationshipService.batchCreateRelationships(relationships);
            return Result.success(created);
        } catch (Exception e) {
            log.error("批量创建关系失败", e);
            return Result.error("批量创建关系失败: " + e.getMessage());
        }
    }
}